package cn.zwx.learn.basic.groovy.utils;

import cn.zwx.learn.basic.groovy.handler.GroovyHandler;
import cn.zwx.learn.basic.groovy.register.GroovyRegister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description:
 * @projectName:java-basic
 * @see:groovy.utils
 * @author:zhangwenxue
 * @createTime:2021/4/13 19:08
 * @version:1.0
 */
public class GroovyUtil {

    private static final Logger logger = LoggerFactory.getLogger(GroovyUtil.class);


    public static GroovyRegister groovyRegister = GroovyRegister.getInstance();


    private static String getClsGroovy(String action) {
        return String.format("Cls_%s", action);
    }



    /**
     * @description 封装调用方法
     * @param action groovy脚本
     * @param param  请求接口参数
     * @return java.lang.Object 返回接口参数
     * @author zhangwenxue
     * @createTime 2021/4/13 19:52
     **/
    public static Object invoke(String action, Object param){
        String groovyFunc = getClsGroovy(action);
        GroovyHandler handler = groovyRegister.get(groovyFunc);
        if (handler == null) {
            logger.error(String.format("找不到对应的groovy类(%s)", groovyFunc));
            throw new RuntimeException(String.format("找不到对应的groovy类(%s)", groovyFunc));
        }
        return handler.invoke(param);
    }

}
